package com.szholly.pro.watergas_server.ps.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Service;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.util.TwoTuple;
import com.szholly.pro.watergas_server.common.web.MessageException;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

import lrq.common.StringUtil;

/**
 * 基础数据查询
 * @author gislin
 * 2016年8月15日
 */
@Service
public class BaseInfoService {

	/**
	 * 获取流域
	 * gislin
	 * 2016年8月15日
	 * @return
	 */
	public List<RowBase> queryRiverBasin(){
		String sql = "  select t.river_basin_code,t.river_basin_name from river_basin_info t where 1=1";
		List<RowBase> result = DataUtil.getSelectRows(sql, null);
		return result;
	}
	
	/**
	 * 获取河流信息
	 * gislin
	 * 2016年8月15日
	 * @param riverBasinCode
	 * @return
	 * @throws MessageException
	 */
	public List<RowBase> queryRiver(String[] riverBasinCodes) throws MessageException{
		
		if(riverBasinCodes == null || riverBasinCodes.length == 0){
			throw new MessageException("流域编号不能为空");
		}
		
		String sql = "  select t.river_code,t.river_name from river_info t where t.river_basin_code in(";
		List<GlobalParameter> pc = new ArrayList<GlobalParameter>();
		
		for(int i=0;i<riverBasinCodes.length;i++){
			sql+="@riverBasinCode"+i+",";
			pc.add(new GlobalParameter("riverBasinCode"+i,riverBasinCodes[i]));
		}
		sql = StringUtil.replaceLast(sql, ",", ")");
		List<RowBase> result = DataUtil.getSelectRows(sql, pc);
		return result;
	}
	
	/**
	 * 获取人员角色信息
	 * gislin
	 * 2016年8月23日
	 * @return
	 */
	public List<RowBase> queryUserRole(String orgId,String userId){
		String sql = "select t1.c_rolename,t1.i_roleid, case when t2.s_roleid is null then '0' else '1' end ischeck" +
						" from safe_role t1\n" +
						"       left join safe_userrole t2 on t1.i_roleid = t2.s_roleid and t2.s_userid = @userId\n" + 
						"       where t1.s_company = @orgId\n";
		List<GlobalParameter> pc = new ArrayList<>();
		pc.add(new GlobalParameter("userId", userId));
		pc.add(new GlobalParameter("orgId", orgId));

		List<RowBase> result = DataUtil.getSelectRows(sql, pc);
		return result;
	}
	
	/**
	 * 获取区和街道数据
	 * @return
	 */
	public TwoTuple<List<RowBase>, HashMap<String, List<RowBase>>> getDistrict(){
		String getJdSql = "select "+
						"       t.jdname name,\n" + 
						"		t.xiancode code,"+
						"       t.shape.minx minx,\n" + 
						"       t.shape.miny miny,\n" + 
						"       t.shape.maxx maxx,\n" + 
						"       t.shape.maxy maxy from sde.jd t\n" + 
						"		left join sde.qx t2 on  t2.xiancode = t.xiancode "+
						" order by t2.xiancode";
		
		String getQuSql = " SELECT  t1.shape.minx minx,\n" +
						"		t1.xiancode code,"+
						"       t1.shape.miny miny,\n" +
						"       t1.shape.maxx maxx,\n" +
						"       t1.shape.maxy maxy,\n" +
						"       T1.XIANNAME name FROM  sde.qx t1";
		
		List<RowBase> quList = DataUtil.getSelectRows(getQuSql, null);
		List<RowBase> jdList = DataUtil.getSelectRows(getJdSql, null);
		
		RowBase allQu = new RowBase();
		allQu.setValue("MINX", 77000.64811086608);
		allQu.setValue("MAXX", 178600.85131127248);
		allQu.setValue("MINY", 5840.093449772013);
		allQu.setValue("MAXY", 57169.36277497733);
		allQu.setValue("CODE", "all");
		allQu.setValue("NAME", "深圳市");
		quList.add(0, allQu);
		
		HashMap<String, List<RowBase>> jdHashMap = new HashMap<>();
		
		for(int i=0;i<jdList.size();i++){
			RowBase jdItem = jdList.get(i);
			String code = jdItem.getSafeString("CODE");
			if(jdHashMap.containsKey(code)){
				jdHashMap.get(code).add(jdItem);
			}
			else{
				jdHashMap.put(code, new ArrayList<>());
			}
		}
		
		TwoTuple<List<RowBase>, HashMap<String, List<RowBase>>> district = new TwoTuple<List<RowBase>, HashMap<String,List<RowBase>>>(quList,jdHashMap);
		return district;
	}
	
}
